﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XmlTest
{
    class Tree : ObjectChildren
    {
        private Group group;
        private IndexedFaceSet indexedFaceSet;
        private Transform transform;
        private IndexedLineSet indexedLineSet;
        private Background background;

        public Background Background
        {
            get { return background; }
            set { background = value; }
        }

        public IndexedLineSet IndexedLineSet
        {
            get { return indexedLineSet; }
            set { indexedLineSet = value; }
        }

        public Transform Transform
        {
            get { return transform; }
            set { transform = value; }
        }

        public IndexedFaceSet IndexedFaceSet
        {
            get { return indexedFaceSet; }
            set { indexedFaceSet = value; }
        }

        public Group Group
        {
            get { return group; }
            set { group = value; }
        }

        private List<XmlTest.Appearance> appearance;

        public List<XmlTest.Appearance> Appearance
        {
            get { return appearance; }
            set { appearance = value; }
        }

        private System.Collections.Generic.List<XmlTest.Shape> shape;

        public System.Collections.Generic.List<XmlTest.Shape> Shape
        {
            get { return shape; }
            set { shape = value; }
        }

        private List<XmlTest.Coordinate> coordinate;

        public List<XmlTest.Coordinate> Coordinate
        {
            get { return coordinate; }
            set { coordinate = value; }
        }
        public static Group getTreeGroup()
        {
            Group group = new Group();
            group.DEF = "group0";

            Background background = new Background();
            group.addChildren(background);
            //第一片叶子
            Shape shape = new Shape();
            shape.DEF = "L0";
            Coordinate coordinate = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
            IndexedFaceSet indexedFaceSet = new IndexedFaceSet();
            indexedFaceSet.coordIndex = "0 1 2 3";
            indexedFaceSet.Coordinate = coordinate;

            Appearance appearance = new Appearance();
            appearance.material = new Material("0 1 0");

            shape.gemeotry = indexedFaceSet;
            shape.appearance = appearance;
            group.addChildren(shape);
            //第二片叶子
            Transform transform = new Transform();
            transform.rotation = "0 1 0 3.14";
            transform.translation = "0 1 0";
            transform.addChildren(new Shape("L0"));
            group.addChildren(transform);
            //树干
            Shape shape2 = new Shape();
            shape2.DEF = "";
            IndexedLineSet indexedLineSet = new IndexedLineSet();
            indexedLineSet.coordIndex = "0 1";
            indexedLineSet.Coordinate = new Coordinate("0 0 0 0 2 0");

            Appearance app2 = new Appearance();
            app2.material.emissiveColor = "0.5 0.5 0";
            shape2.appearance = app2;
            shape2.gemeotry = indexedLineSet;
            group.addChildren(shape2);
            return group;
        }

        public static Scene getTreeSence(Group group0, int deep)
        {
            Scene sence = new Scene();
            sence.addChildren(group0);

            for (int i = 1; i <= deep; i++)
            {
                Group tempgroup = new Group();
                tempgroup.DEF = "group" + i;
                Transform[] transform = new Transform[5];
                transform[0] = new Transform();
                transform[0].DEF = "s" + i;
                transform[0].scale = "0.5 0.5 0.5";
                transform[0].addChildren(new Group("group" + (i - 1)));

                transform[1] = new Transform();
                transform[1].translation = "0 1 0";
                transform[1].addChildren(new Transform("s" + i));

                transform[2] = new Transform();
                transform[2].DEF = "g" + i;
                transform[2].rotation = "1 0 0 0.75";
                transform[2].translation = "0 1.7 0";
                transform[2].addChildren(new Transform("s" + i));

                transform[3] = new Transform();
                transform[3].rotation = "0 1 0 2.09";
                transform[3].addChildren(new Transform("g" + i));

                transform[4] = new Transform();
                transform[4].rotation = "0 1 0 -2.09";
                transform[4].addChildren(new Transform("g" + i));

                foreach (Transform temp in transform)
                {
                    tempgroup.addChildren(temp);
                }

                sence.addChildren(tempgroup);

            }
            return sence;
        }
        /*            "<Group DEF='group0'>\n
         * <Shape DEF='L0'>\n"+
                      "<IndexedFaceSet coordIndex='0 1 2 3' solid='false'>\n"+
                      "<Coordinate point='0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2' />\n"+
                      "</IndexedFaceSet>\n
         * <Appearance>\n<Material diffuseColor='"+r+" "+g+" "+b+"' />\n"+
                      "</Appearance>\n
         * </Shape>\n
         * <Transform rotation='0 1 0 3.14' translation='0 1 0'>\n"+
                      "<Shape USE='L0' />\n
         * </Transform>\n
         * <Shape>\n
         * <IndexedLineSet coordIndex='0 1'>\n"+
                      "<Coordinate point='0 0 0 0 "+height+" 0' />\n
         * </IndexedLineSet>
         * <Appearance>\n"+
                      "<Material emissiveColor='0.5 0.5 0' />\n
         * </Appearance>\n</Shape>\n"+
                      "</Group>\n";*/
        public Tree()
        {
            //this.group = new Group();
            //Group.DEF = "group0";
            //this.shape = new Shape();
            //Shape.DEF = "L0";

            //Coordinate coordinate = new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2");
            //this.indexedFaceSet= new IndexedFaceSet();
            //IndexedFaceSet.coordIndex = "0 1 2 3";
            //IndexedFaceSet.Coordinate = coordinate;

            //this.appearance = new Appearance();
            //appearance.material = new Material("0 1 0");

            //Shape.gemeotry = this.indexedFaceSet;
            //Group.addChildren(this.shape);
            group = new Group();
            group.DEF = "group0";

            background = new Background();
            group.addChildren(background);
            //第一片叶子
            shape = new List<Shape>{};
            shape.Add( new Shape());
            shape.ElementAt(0).DEF = "L0";
            coordinate = new List<Coordinate>{};
            coordinate.Add( new Coordinate("0 1 0 0.4 1.6 0.2 0.8 2.2 0 0.2 1.6 -0.2"));
            IndexedFaceSet indexedFaceSet = new IndexedFaceSet();
            indexedFaceSet.coordIndex = "0 1 2 3";
            indexedFaceSet.Coordinate = coordinate.ElementAt(0);

            appearance = new List<Appearance> { };
            appearance.Add( new Appearance());
            appearance.ElementAt(0).material = new Material("0 1 0");

            shape.ElementAt(0).gemeotry = indexedFaceSet;
            shape.ElementAt(0).appearance = appearance.ElementAt(0);
            group.addChildren(shape.ElementAt(0));
            //第二片叶子
            transform = new Transform();
            transform.rotation = "0 1 0 3.14";
            transform.translation = "0 1 0";
            transform.addChildren(new Shape(shape.ElementAt(0).DEF));
            group.addChildren(transform);
            //树干
            shape.Add( new Shape());
            shape.ElementAt(1).DEF = "";
            IndexedLineSet indexedLineSet = new IndexedLineSet();
            indexedLineSet.coordIndex = "0 1";
            coordinate.Add(new Coordinate("0 0 0 0 2 0"));
            indexedLineSet.Coordinate = coordinate.ElementAt(1);

            appearance.Add( new Appearance());
            appearance.ElementAt(1).material.emissiveColor = "0.5 0.5 0";
            shape.ElementAt(1).appearance = appearance.ElementAt(1);
            shape.ElementAt(1).gemeotry = indexedLineSet;
            group.addChildren(shape.ElementAt(1));
        }

    }
}
